package com.bw.day03.fg

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.CaregoryGoodsEntity
import com.bw.base.net.entity.CateEntity
import com.bw.base.net.entity.GoodsEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch

class CategoryViewModel : BaseViewModel(){
    val cateSuccess = SingleLiveData<Res<CateEntity>>()
    val cateFail = SingleLiveData<String>()
    fun category(){
        viewModelScope.launch {
            try {
                cateSuccess.postValue(apiService.category())
            }catch (e:Exception){
                cateFail.postValue(e.message.toString())
            }
        }
    }
    val goodsSuccess = MutableLiveData<Res<GoodsEntity>>()
    val goodsFail = MutableLiveData<String>()
    fun categoryGoods(cid:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.value = apiService.categoryGoods(cid,limit, page)
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}